package common.helper.json;

/**
 * Created by pkuderc on 5/29/16.
 */
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import common.helper.toolkit.FileToolkit;

import java.io.IOException;


public class JsonToJavaObject {
    public static void main(String[] args){
        String json = FileToolkit.readStringFromFile("/elasticsearch/meizi/video.json");
        System.out.println(json);
        TestJsonToJavaObject(json);
    }

    public static JsonNode loadJsonNode(String json){
        JsonFactory factory = new JsonFactory();
        ObjectMapper mapper = new ObjectMapper(factory);
        mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        JsonNode rootNode = null;
        try {
            rootNode = mapper.readTree(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rootNode;
    }

    public static String jsonNodeToString(JsonNode rootNode){
        String json = "";
        JsonFactory factory = new JsonFactory();
        ObjectMapper mapper = new ObjectMapper(factory);
        mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        try {
            json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    public static void printJsonNode(JsonNode rootNode){
        JsonFactory factory = new JsonFactory();
        ObjectMapper mapper = new ObjectMapper(factory);
        mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        try {
            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }


    public static void TestJsonToJavaObject(String json){
        /*
        http://stackoverflow.com/questions/19760138/parsing-json-in-java-without-knowing-json-format
        http://wiki.fasterxml.com/JacksonTreeModel
         */
        JsonFactory factory = new JsonFactory();
        ObjectMapper mapper = new ObjectMapper(factory);
        /*
        http://www.cowtowncoder.com/blog/archives/2009/08/entry_310.html
        https://blog.getify.com/json-comments/
         */
        mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        /*
        http://stackoverflow.com/questions/17617370/pretty-printing-json-from-jackson-2-2s-objectmapper
        http://www.mkyong.com/java/how-to-enable-pretty-print-json-output-jackson/
         */
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        try {
            JsonNode rootNode = mapper.readTree(json);
            JsonNode mediaNode = rootNode.get("test0");
            JsonNode all_Node = mediaNode.get("_all");

            System.out.println("\n\n");
            /*
            http://www.tutorialspoint.com/jackson/jackson_tree_model.htm
             */
            JsonNode newNode = mapper.createObjectNode();
            ((ObjectNode)newNode).put("video" , mediaNode);
            JsonNode arrNode = mapper.createArrayNode();
            ((ArrayNode)arrNode).add(all_Node);

            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(newNode));
            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(arrNode));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
